# -*- coding: utf-8 -*-
import scrapy
from douban.items import Movie250
from scrapy.http import Request
from urllib.parse import urljoin


class Movie250Spider(scrapy.Spider):
    name = 'movie_250'
    allowed_domains = ['douban.com']
    start_urls = ['https://movie.douban.com/top250']

    def parse(self, response):
        movie_item = Movie250()
        movie_list = response.xpath('//div[@class="info"]')
        for movie in movie_list:
            name = ''.join(movie.xpath('./div[@class="hd"]/a/span/text()').extract())
            director_actor = movie.xpath('./div[@class="bd"]/p[1]/text()').extract()[0].replace('\n', '').strip()
            director = str(director_actor).split('\xa0\xa0\xa0')[0]
            year_type = movie.xpath('./div[@class="bd"]/p[1]/text()').extract()[1].replace('\n', '').lstrip()
            year = year_type[:4]
            type = year_type[6:].lstrip()
            rating = movie.xpath('./div[@class="bd"]/div/span[2]/text()').extract_first()
            rating_num = movie.xpath('./div[@class="bd"]/div/span[4]/text()').extract_first()[:-3]
            quote = movie.xpath('./div[@class="bd"]/p[@class="quote"]/span/text()').extract_first()
            detail = movie.xpath('./div[@class="hd"]/a/@href').extract_first()
            movie_item['name'] = name
            movie_item['director'] = director
            movie_item['year'] = year
            movie_item['type'] = type
            movie_item['rating'] = rating
            movie_item['rating_num'] = rating_num
            movie_item['quote'] = quote
            movie_item['detail'] = detail
            yield movie_item
        next = response.xpath('//span[@class="next"]/a/@href').extract()
        if next:
            next = next[0]
            yield Request(urljoin(response.url, next), callback=self.parse)
